#Region "Imports"
Imports Microsoft.Win32
Imports System
#End Region


Public Class UTIL
    Public Shared Sub DebugException(ByRef ex As System.Exception)
        ' level 0 - function level exceptions
        ' level 1 - current debugging
        ' level 2 - error (that also gets shown to the user)
        ' level 4 - error (that does not get shown to the user)

        Dim trace As System.Diagnostics.StackTrace = New System.Diagnostics.StackTrace(ex, True)
        Dim exceptionLineNumber As Integer = trace.GetFrame(0).GetFileLineNumber()

        Debug.Print("Exception in " & trace.GetFrame(0).GetMethod.Name & vbCrLf & _
         CStr(ex.Message) & "; " & vbCrLf & _
         trace.GetFrame(0).ToString & vbCrLf)

    End Sub


    Public Shared Sub SaveStg( _
     ByRef sApName As String, _
     ByVal sValName As String, _
     ByVal sVal As String)


        Dim regSubAppK As RegistryKey = Nothing
        Try
            Try
                regSubAppK = MainDlg.regAppKey.OpenSubKey(sApName, True)
            Finally
                If regSubAppK Is Nothing Then regSubAppK = MainDlg.regAppKey.CreateSubKey(sApName)
            End Try
            regSubAppK.SetValue(sValName, sVal, RegistryValueKind.String)

        Catch ex As System.Exception
            UTIL.DebugException(ex)


        Finally
            If Not (regSubAppK Is Nothing) Then regSubAppK.Close()
        End Try
    End Sub


    Public Shared Sub SaveStg( _
     ByRef sApName As String, _
     ByVal sValName As String, _
     ByVal sVal As Long)


        Dim regSubAppK As RegistryKey = Nothing
        Try
            Try
                regSubAppK = MainDlg.regAppKey.OpenSubKey(sApName, True)
            Finally
                If regSubAppK Is Nothing Then regSubAppK = MainDlg.regAppKey.CreateSubKey(sApName)
            End Try
            regSubAppK.SetValue(sValName, sVal, RegistryValueKind.DWord)

        Catch ex As System.Exception
            UTIL.DebugException(ex)


        Finally
            If Not (regSubAppK Is Nothing) Then regSubAppK.Close()
        End Try
    End Sub


    Public Shared Sub SaveStg( _
     ByRef sApName As String, _
     ByVal sValName As String, _
     ByVal sVal As Boolean)


        Dim regSubAppK As RegistryKey = Nothing
        Try
            Try
                regSubAppK = MainDlg.regAppKey.OpenSubKey(sApName, True)
            Finally
                If regSubAppK Is Nothing Then regSubAppK = MainDlg.regAppKey.CreateSubKey(sApName)
            End Try
            regSubAppK.SetValue(sValName, sVal, RegistryValueKind.DWord)

        Catch ex As System.Exception
            UTIL.DebugException(ex)


        Finally
            If Not (regSubAppK Is Nothing) Then regSubAppK.Close()
        End Try
    End Sub


    Public Shared Sub SaveStg( _
     ByRef sApName As String, _
     ByVal sValName As String, _
     ByVal sVal As Double)


        Dim regSubAppK As RegistryKey = Nothing
        Try
            Try
                regSubAppK = MainDlg.regAppKey.OpenSubKey(sApName, True)
            Finally
                If regSubAppK Is Nothing Then regSubAppK = MainDlg.regAppKey.CreateSubKey(sApName)
            End Try
            regSubAppK.SetValue(sValName, sVal.ToString, RegistryValueKind.String)

        Catch ex As System.Exception
            UTIL.DebugException(ex)


        Finally
            If Not (regSubAppK Is Nothing) Then regSubAppK.Close()
        End Try
    End Sub



    Public Shared Function GetLongStg( _
     ByRef sApName As String, _
     ByRef sValName As String, _
     ByVal iDefVal As Long) As Long

        Dim iRetV As Long
        Dim regSubAppK As RegistryKey = Nothing
        Try
            Try
                regSubAppK = MainDlg.regAppKey.OpenSubKey(sApName, False)
            Finally
                If regSubAppK Is Nothing Then regSubAppK = MainDlg.regAppKey.CreateSubKey(sApName)
            End Try
            Try
                iRetV = regSubAppK.GetValue(sValName, iDefVal)
            Catch
                iRetV = iDefVal
            End Try

        Catch ex As System.Exception
            UTIL.DebugException(ex)
            iRetV = iDefVal

        Finally
            If Not (regSubAppK Is Nothing) Then regSubAppK.Close()
        End Try
        Return iRetV
    End Function


    Public Shared Function GetStrStg( _
       ByRef sApName As String, _
       ByRef sValName As String, _
       ByVal sDefVal As String) As String

        Dim sRetV As String
        Dim regSubAppK As RegistryKey = Nothing
        Try
            Try
                regSubAppK = MainDlg.regAppKey.OpenSubKey(sApName, False)
            Finally
                If regSubAppK Is Nothing Then regSubAppK = MainDlg.regAppKey.CreateSubKey(sApName)
            End Try
            Try
                sRetV = regSubAppK.GetValue(sValName, sDefVal)
            Catch
                sRetV = sDefVal
            End Try

        Catch ex As System.Exception
            UTIL.DebugException(ex)
            sRetV = sDefVal

        Finally
            If Not (regSubAppK Is Nothing) Then regSubAppK.Close()
        End Try
        Return sRetV
    End Function

End Class
